<?php
	// En base a: http://buildinternet.com/2009/12/creating-your-first-php-application-part-1/
	// Modificación de la capa de persistencia para usar PDO en lugar de mysql.
class DB {
	
	//protected $db_name = 'test.sqlite';
	protected $db_user = 'user';
	protected $db_pass = 'user';
	//protected $db_host = 'localhost';
	//protected $dsn = 'sqlite:test.sqlite';
	protected $dsn = 'mysql:dbname=7567tp1;host=127.0.0.1';
	protected $connection = null;
	
	//open a connection to the database. Make sure this is called 
	//on every page that needs to use the database.
	public function connect() {
		//$connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
		//mysql_select_db($this->db_name);
		
		$this->connection = new PDO($this->dsn, $this->db_user, $this->db_pass);
		//$this->connection = new PDO($this->dsn);		
		return true;
	}
	
	//takes a mysql row set and returns an associative array, where the keys
	//in the array are the column names in the row set. If singleRow is set to
	//true, then it will return a single row instead of an array of rows.
	public function processRowSet($rowSet, $one)
	{
		$resultArray = array();
		//while($row = mysql_fetch_assoc($rowSet))
		while ($row = $rowSet->fetch(PDO::FETCH_ASSOC))
			array_push($resultArray, $row);

		return ($resultArray);
	}
	
	//Select rows from the database.
	//returns a full row or rows from $table using $where as the where clause.
	//return value is an associative array with column names as keys.
	//TODO: columList agregado por Ale, es necesario probarlo.
	public function select($table, $where = null, $columnList = array()) {
		$columns = "*";
		if (count($columnList) > 0)
			$columns = implode(",",$this->connection->quote($columnList));
		
		$sql = "SELECT $columns FROM $table";
		if (isset($where))
			$sql.= " WHERE $where";
			
		error_log($sql);
		//$result = mysql_query($sql);
		$result = $this->connection->query($sql);

		if (!$result) {
			die( implode(" ",$this->connection->errorInfo()) );
			return null;
		}

		return $this->processRowSet($result, count($result) == 1);
	}
	
	//Updates a current row in the database.
	//takes an array of data, where the keys in the array are the column names
	//and the values are the data that will be inserted into those columns.
	//$table is the name of the table and $where is the sql where clause.
	// Return number of affected rows.
	public function update($data, $table, $where) {
		$result = 0;
		foreach ($data as $column => $value) {
			$sql = "UPDATE $table SET $column = $value WHERE $where";
			//mysql_query($sql) or die(mysql_error());
			$result = $this->connection->exec($sql) or die($this->connection->errorInfo());
		}
		return $result;
	}
	
	//Inserts a new row into the database.
	//takes an array of data, where the keys in the array are the column names
	//and the values are the data that will be inserted into those columns.
	//$table is the name of the table.
	public function insert($data, $table) {
		#TODO PASAR TODOS LAS SENTENCIAS A QUE USEN PREPARE.
		$columns = "";
		$columnsVars = "";
		$values = "";
		$aux = array();
		foreach ($data as $column => $value) {
			$columns .= ($columns == "") ? "" : ", ";
			$columns .= $column;
			$columnsVars .= ($columnsVars == "") ? "" : ", ";
			$columnsVars .= ":".$column;
			$values .= ($values == "") ? "" : ", ";
			$values .= $value;

			$aux[":".$column] = $value;
		}

		$sql = "INSERT INTO $table ($columns) VALUES($columnsVars)";
		error_log($sql);	

		$query = $this->connection->prepare($sql);

		//mysql_query($sql) or die(mysql_error());
		$exito = $query->execute($aux);
		if (!$exito)
			die( join(" ",$query->errorInfo()) );

		//return the ID of the user in the database.
		//return mysql_insert_id();
		return $this->connection->lastInsertId($table);
	}
	
}

?>
